


import 'package:bike_ride/api/common_repository.dart';
import 'package:bike_ride/common/mvvm/base_view_model.dart';
import 'package:bike_ride/common/util/account_manager.dart';
import 'package:bike_ride/common/util/model_utils.dart';
import 'package:bike_ride/common/widget/notifier_widget.dart';
import 'package:bike_ride/model/mine/view/mine_v.dart';
import 'package:dio/dio.dart';

class MineViewModel extends PageViewModel<MineViewState> {
  CancelToken? cancelToken;

  ///余额
  num? balance;
  num? countCards;
  num? countCoupons;

  @override
  onCreate() {
    cancelToken = CancelToken();
    pageDataModel = PageDataModel();
    getUserInfo();
    getFinanceInfo();
    getStatDiscountInfo();
  }

  @override
  onDispose() {
    if (!(cancelToken?.isCancelled ?? true)) {
      cancelToken?.cancel();
    }
    super.onDispose();
  }

  Future getUserInfo() async {
    final result = await CommonRepository.getUserInfoIdl(cancelToken: cancelToken);
    if(result != null){
      AccountManager.userInfo = result;
    }
    state.notifyChange();
  }

  Future getStatDiscountInfo() async {
    final result = await CommonRepository.getStatDiscountIdl(cancelToken: cancelToken);
    if(result != null){
      countCards = result.countCards;
      countCoupons = result.countCoupons;
    }
    state.notifyChange();
  }

  Future getFinanceInfo() async {
    final result = await CommonRepository.getFinanceInfoIdl(cancelToken: cancelToken);
    final addT = (result?.balanceAmount??0) + (result?.giftBalanceAmount??0);
    balance = parseToMoney(addT);
    state.notifyChange();
  }

  /// 请求数据
  @override
  Future<PageViewModel?> requestData({Map<String, dynamic>? params}) async {
    return null;

  }
}